﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Data;
using Wicresoft.SMS.Web.App_GlobalResources;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.Net;
using Wicresoft.SMS.Common;

namespace Wicresoft.SMS.Web.Controllers
{
    public class AccountController : CustomBaseController
    {
        IUserRoleService _service;
        public AccountController(IUserRoleService service)
        {
            _service = service;
        }

        //
        // GET: /account/login
        public ViewResult LogIn()
        {
            AccountLogInModel model = new AccountLogInModel();
            model.CheckRemember = false;
            model.HiddenReturnUrl = Request.QueryString.Get("ReturnUrl");
            //model.InputUserName = "Dada LIAO";
            //model.InputPassword = "Password01!";

            if (CultureProvider.GetCurrentCultureString() == "zh-cn")
            {
                model.IsZHCN = true;
                model.Url = Request.Path.Replace("zh-cn", "en-us");
            }
            else
            {
                model.IsZHCN = false;
                model.Url = Request.Path.Replace("en-us", "zh-cn");

            }

            ViewData.Model = model;
            return View();
        }

        //TODO
        [HttpPost]
        public ActionResult LogIn(AccountLogInModel model)
        {
            if (model.InputUserName == null || model.InputPassword == null)
            {
                return JavaScript(string.Format("alert(\"{0}\")", Resources.Controller_Account_Login));
            }

            if (model.InputUserName.Trim() == "" || model.InputPassword.Trim() == "")
            {
                return JavaScript(string.Format("alert(\"{0}\")", Resources.Controller_Account_Login));
            }

            WebConfigProvider wcp = new WebConfigProvider();

            ICILHelper ich = new CILHelper(wcp.CILServer, wcp.HRConnectionString);
            var result = ich.ValidationUser(model.InputUserName, model.InputPassword);

            // var result = _service.ValidationUser(model.InputUserName, model.InputPassword);
            if (result)
            {
                InitalUserInfo(ich.GetUserInfoByCIL(model.InputUserName));

                _service.LogIn(model.InputUserName, model.CheckRemember);
                //string url = _service.GetRedirectUrl(model.InputUserName, model.CheckRemember);
                string url = model.HiddenReturnUrl;
                if (!string.IsNullOrEmpty(url))
                    return JavaScript(string.Format("top.location =\"{0}\"", url));
                // return Redirect(url);
                else
                    return JavaScript(string.Format("top.location = \"{0}\" ", string.Format(@"/{0}/home/index", CultureProvider.GetCurrentCultureString())));
                //return RedirectToAction("index", "home");
            }
            else
            {
                ViewData.Model = model;
                //  ViewData.ModelState.AddModelError("InputUserName", "hi");//Resources.login_msg_loginfailed_summary);
                return JavaScript(string.Format("alert(\"{0}\")", Resources.Controller_Account_Login));
            }
            return View("LogInPartial");
        }

        private void InitalUserInfo(Common.C_evw_Employee emp)
        {
            if (emp != null)
            {

                IUserService ius = new UserService(new WebConfigProvider());

                var user = ConvertCILUser2DataUser(emp, ius.GetUserItemByCIL(emp.CILX500));

                ius.CreateOrUpdateUser(user);
            }
        }

        private Data.User ConvertCILUser2DataUser(Common.C_evw_Employee emp, Data.User user)
        {
            if (user == null)
            {
                user = new User();
                user.IsActive = 0;
                user.DepartmentID = 2;
            }
            user.CIL = emp.CILX500;
            user.NameCN = emp.NameCN;
            user.NameEN = emp.NameEN;
            user.Email = emp.Email;
            user.DeptName = emp.DeptName;
            user.CSL = emp.NTacount;
            return user;
        }
        public ActionResult LogOut()
        {
            _service.LogOut();
            return Redirect("~/");
        }

    }
}
